package net.ithe.controller;


import lombok.extern.slf4j.Slf4j;
import net.ithe.anno.Log;
import net.ithe.pojo.Dept;
import net.ithe.pojo.Result;
import net.ithe.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j//由于用来Lombok可以直接使用注解的方式
@RequestMapping("/depts")
@RestController
public class DeptController {
    //日志输出
    //private static  final Logger log= LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;
    //@RequestMapping(value ="/depts" ,method = RequestMethod.GET )//指定请求方式
    @GetMapping
    public Result list(){
        //System.out.println("查询所有部门数据");
        log.info("查询所有部门数据");
        List<Dept> deptList=deptService.findAll();
        return Result.success(deptList);
    }
    /**
     * 删除部门
     */
//    @DeleteMapping(value = "/depts")
//    public Result delete(HttpServletRequest request){
//        String iDstr = request.getParameter("id");
//        int id = Integer.parseInt(iDstr);
//        System.out.println("根据id删除部门"+id);
//        return Result.success();
//    }
    /**
     * 删除部门方式二
     */
    @Log
    @DeleteMapping
    public Result delete(@RequestParam("id") Integer id){
        //System.out.println("根据id删除部门"+id);
        log.info("根据id删除部门{}",id);
        deptService.deleteById(id);
        return Result.success();
    }
    /**
     * 新增部门
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        //System.out.println("新增部门"+dept);
        log.info("新增部门{}",dept);
        deptService.add(dept);
        return Result.success();
    }
    /**
     * 根据id查询部门
     */
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Integer id){
        //System.out.println("根据id查询部门"+id);
        log.info("根据id查询部门{}",id);
        Dept dept=deptService.getById(id);
        return Result.success(dept);
    }
    /**
     * 修改部门
     */
    @Log
   @PutMapping
    public Result update(@RequestBody Dept dept){
        //System.out.println("修改部门"+dept);
       log.info("修改部门{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
